<?php

namespace shop\service;

use Yii;

class UserService
{

    /**
     * 获取目标小程序的appid
     * @param $where
     * @return array|false
     */
    public function userOne($where)
    {
        $sql = "SELECT mall.id, collect.openid, mall.mall_total_fee FROM edu_user AS mall LEFT JOIN mini_collect_user AS collect ON mall.id = collect.uid WHERE {$where}";
        return Yii::$app->db->createCommand($sql)->queryOne();
    }

    public function userAppear($map)
    {
        $db = Yii::$app->db;
        $transaction = $db->beginTransaction();
        try {
            $db->createCommand()->insert('mall_appear', $map)->execute();
            //同时将金额放入到冻结中
            $sql = "UPDATE edu_user SET mall_total_fee = mall_total_fee - {$map['amount']}, mall_put_money = mall_put_money + {$map['amount']} WHERE id = {$map['uid']}";
            $db->createCommand($sql)->execute();

            $transaction->commit();
            return true;
        } catch (\Exception $e) {
            $transaction->rollBack();
//            throw $e;
            return false;
        }
    }
}